package com.hermes.action.subject;


import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.hermes.biz.common.model.comment.UserCommentDisplayUnitDO;
import com.hermes.biz.common.model.item.ItemDO;
import com.hermes.biz.common.model.result.AsynResultDO;
import com.hermes.biz.common.model.result.UserResultDO;
import com.hermes.biz.common.model.user.UserCommentDO;
import com.hermes.biz.common.model.user.UserDO;
import com.hermes.biz.common.model.user.UserPublicDO;
import com.hermes.biz.common.util.string.StringUtil;
import com.hermes.biz.manager.user.UserCommentManager;
import com.hermes.biz.manager.user.impl.UserOperateManagerImpl;
import com.hermes.biz.service.item.ItemService;
import com.hermes.biz.service.item.UserItemRelationService;
import com.hermes.biz.service.user.UserService;
import com.hermes.common.action.BaseAction;
import com.hermes.common.util.ParameterParserUtil;


/**
 * 
 * @author  Heng
 */
public class SubjectSwitch extends BaseAction {


	private static final long serialVersionUID = -6528111422997258208L;
		
	private Logger log = Logger.getLogger(SubjectSwitch.class);
			
	private ItemService itemService;
	
	private UserService userService;
	
	private UserCommentManager userCommentManager ;

	private UserItemRelationService userItemRelationService;

	private List<AsynResultDO> resultList;

	public UserPublicDO userPublicDO =new UserPublicDO();
	
	
	

	public UserOperateManagerImpl userOperateManager;				
	
	private final int numToDis=10;

	public String username="";

	private String isHome;

	private String currentTag;
	
	public boolean isLogin = true;
	
	public long userid;
	
	public int tagId = 0;
	
	public int logoyet = 0;
	
	public String url="index.htm";

	@Override
	public String execute() {
				
		long timebegin=System.currentTimeMillis();
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		try{
				username = (String) session.getAttribute("userName");
				userid = (Long) session.getAttribute("userId");	
				if(userid<=0){
					isLogin = false;
				}else{
				UserResultDO userResultDo =	userOperateManager.getSingleUserByUserId(Long.valueOf(userid));
				UserDO userDO = userResultDo.getModule();
				logoyet = userDO.getLogoyet();
				
				userPublicDO.setHeadPic(userDO.getHeadPic());
				userPublicDO.setUserName(userDO.getUserName());
				
				session.setAttribute("userPublicDO", userPublicDO);
		}
					
		}catch(Exception e){
				log.info(e.toString());
		}
		if(username==null||username==""){
			setLogin(false);
		}

		setIsHome("true");
		String tag = ParameterParserUtil.getParameter("tagId");
		//
		if(StringUtil.isNotBlank(tag)&& StringUtil.isNumOnly(tag)){
			url = "index.htm?tagId="+tag;
		}	
		try {
			url = URLEncoder.encode(url, "GBK");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				
		int nextPageInt=0;
		String nextPage = ParameterParserUtil.getParameter("next");
		if(StringUtil.isNotBlank(nextPage)){
			nextPageInt = Integer.parseInt(nextPage);
		}
			
		resultList = new ArrayList<AsynResultDO>();
								
		currentTag="0";
				
		if(StringUtil.isNumOnly(tag)){
					
					tagId = Integer.parseInt(tag);
					if(tagId!=0){
						setIsHome("false");
						currentTag=tag;
					}
					
					long totalTagNum = itemService.countItemNumber(tagId);
					log.error("total-number-tag"+totalTagNum);
					List<ItemDO> result = itemService.getItemByItemTagId(tagId,nextPageInt*numToDis,nextPageInt*numToDis+numToDis);
					//log.error("result-size:"+result.size());
					
					for(ItemDO item:result){
						AsynResultDO temp = new AsynResultDO();
						temp.setItemPic(item.getItemPicUrl());
						temp.setItemTitle(item.getItemTitle());
						temp.setDescription(item.getDescription());
						temp.setItemId(item.getItemId());
						temp.setTagId(item.getTagId());
						temp.setBeLovedNum(item.getBelovedNum());
						log.error("item-love-num:"+item.getBelovedNum());
						temp.setDescription_simple(item.getDescription_simple());
						
						if(isLogin){
							boolean hasLoved = userItemRelationService.hasUserInterestedItem(userid, item.getItemId());
							if(hasLoved){
								temp.setIsLoved(1);
							}
						}
						
						List<UserCommentDO> userComments = userCommentManager.getUserCommentInfoByItemIdV2(item.getItemId());
								
						if(userComments!=null && userComments.size()>0){

							for(UserCommentDO commentDO:userComments){
								UserCommentDisplayUnitDO commentUnit = new UserCommentDisplayUnitDO();
								commentUnit.setComment(commentDO.getComment());
								commentUnit.setCommentId(commentDO.getUserCommentId());
								commentUnit.setHeadPic(userService.getUserInfoByUserId(commentDO.getCreateUserId()).getModule().getHeadPic());
								commentUnit.setUserName(userService.getUserInfoByUserId(commentDO.getCreateUserId()).getModule().getUserName());
								temp.getComments().add(commentUnit);
								
								log.error("comment-detail----------:"+commentUnit.toString());
							}

						}
						int col = 2;
						if(item.getItemId()%2==0){
							col = 3;
						}
						temp.setCol(col);
						resultList.add(temp);
						
					}
					log.error("result-list-orgin-size:"+resultList.size());
					return SUCCESS;
		}
				
		List<ItemDO> result = itemService.getItemByItemTagId(1,nextPageInt*numToDis,nextPageInt*numToDis+numToDis);
				
		if(result!=null){
					for(ItemDO item:result){
						AsynResultDO temp = new AsynResultDO();
						temp.setItemPic(item.getItemPicUrl());
						temp.setItemTitle(item.getItemTitle());
						temp.setDescription(item.getDescription());
						temp.setItemId(item.getItemId());
						temp.setTagId(item.getTagId());
						temp.setBeLovedNum(item.getBelovedNum());
						temp.setDescription_simple(item.getDescription_simple());
						List<UserCommentDO> userComments = userCommentManager.getUserCommentInfoByItemId(item.getItemId());
						if(userComments!=null && userComments.size()>0){
							for(UserCommentDO commentDO:userComments){
								UserCommentDisplayUnitDO commentUnit = new UserCommentDisplayUnitDO();
								commentUnit.setComment(commentDO.getComment());
								commentUnit.setCommentId(commentDO.getUserCommentId());
								commentUnit.setUserId(commentDO.getCreateUserId());
								commentUnit.setHeadPic(userService.getUserInfoByUserId(commentDO.getCreateUserId()).getModule().getHeadPic());
								commentUnit.setUserName(userService.getUserInfoByUserId(commentDO.getCreateUserId()).getModule().getUserName());
								temp.getComments().add(commentUnit);
							}
						}					
						resultList.add(temp);
					}
		}
				
		long timeend=System.currentTimeMillis();
		log.error("*********index-time-consum-milsec*********:"+(timeend-timebegin));
		log.error("*********index-time-consum*********:"+(timeend-timebegin)/1000);
		log.error("result-list-size:"+resultList.size());
		return SUCCESS;
	}
		
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public long getUserid() {
		return userid;
	}

	public void setUserid(long userid) {
		this.userid = userid;
	}

	public ItemService getItemService() {
		return itemService;
	}
		
	public void setItemService(ItemService itemService) {
		this.itemService = itemService;
	}
			
	public List<AsynResultDO> getResultList() {
		return resultList;
	}

	public void setResultList(List<AsynResultDO> resultList) {
		this.resultList = resultList;
	}

	public String getIsHome() {
		return isHome;
	}

	public void setIsHome(String isHome) {
		this.isHome = isHome;
	}
	public int getTagId() {
		return tagId;
	}

	public void setTagId(int tagId) {
		this.tagId = tagId;
	}
	
	public boolean isLogin() {
		return isLogin;
	}

	public void setLogin(boolean isLogin) {
		this.isLogin = isLogin;
	}

	public String getCurrentTag() {
		return currentTag;
	}

	public void setCurrentTag(String currentTag) {
		this.currentTag = currentTag;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

			
			public int getLogoyet() {
				return logoyet;
			}

			public void setLogoyet(int logoyet) {
				this.logoyet = logoyet;
			}			
			public UserOperateManagerImpl getUserOperateManager() {
				return userOperateManager;
			}

			public void setUserOperateManager(UserOperateManagerImpl userOperateManager) {
				this.userOperateManager = userOperateManager;
			}
			
			
			public UserCommentManager getUserCommentManager() {
				return userCommentManager;
			}

			public void setUserCommentManager(UserCommentManager userCommentManager) {
				this.userCommentManager = userCommentManager;
			}

			public UserItemRelationService getUserItemRelationService() {
				return userItemRelationService;
			}

			public void setUserItemRelationService(
					UserItemRelationService userItemRelationService) {
				this.userItemRelationService = userItemRelationService;
			}
			public UserPublicDO getUserPublicDO() {
				return userPublicDO;
			}

			public void setUserPublicDO(UserPublicDO userPublicDO) {
				this.userPublicDO = userPublicDO;
			}

			

}
